package com.xyw.code.springcloudgateway1;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.gateway.filter.factory.AddRequestHeaderGatewayFilterFactory;
import org.springframework.cloud.gateway.route.RouteLocator;
import org.springframework.cloud.gateway.route.builder.RouteLocatorBuilder;
import org.springframework.context.annotation.Bean;

@SpringBootApplication
public class SpringCloudGateway1Application {

    public static void main(String[] args) {
        SpringApplication.run(SpringCloudGateway1Application.class, args);
    }

    @Bean
    public RouteLocator customerRouteLocator(RouteLocatorBuilder builder){
        return builder.routes().route(r->{
            //记得加** 就表示之后的转发转发到http://localhost:8001/** 上面是个正则
            return r.path("/customr/**")
                    .filters(f->{
                        //进入过滤的自定义类是RequestTimeFilter
                        return  f.filter(new RequestTimeFilter()).addRequestHeader("X-Response-Default-Foo", "Default-Bar")
                                .stripPrefix(1);
                        //去除customr前缀
//                        f.stripPrefix(1);
//                        return f;
                    })
                    .uri("http://localhost:8001")
                    .order(0)
                    .id("customer_filter_router");
        }).build();
    }

    @Bean
    public RequestTimeGatewayFilterFactory elapsedGatewayFilterFactory() {
        return new RequestTimeGatewayFilterFactory();
    }

}
